<?php
namespace extend;

use lib\Request;

class Page{

    /**
     * 制作页码
     * @param $now
     * @param $all
     * @param int $left
     * @param int $right
     * @return array
     */
    static function buildPage($now, $all, $left = 5, $right = 5){
        $pages = array();
        $start = $now - $left <= 1 ? 1 : $now - $left;
        $ended = $now + $right >= $all ? $all : $now + $right;
        for($i = $start; $i <= $ended; $i++){
            if($i == $now){
                $pages['now'] = $i;
            }else{
                $pages[] = $i;
            }
        }
        return $pages;
    }

    /**
     * 下一页的页码
     * @param $allPage
     * @return int|null
     */
    static function next($allPage){
    	$request = Request::instance();
        $now = $request->data['page'] ? $request->data['page'] : 1;
        return ($now + 1) >= $allPage ? $allPage : $now + 1;
    }

    /**
     * 上一页页码
     * @return int|null
     */
    static function prev(){
	    $request = Request::instance();
        $now = $request->data['page'] ? $request->data['page'] : 1;
        return ($now - 1) <= 1 ? 1 : $now - 1;
    }

}